package com.sqkj.dao;

import com.sqkj.entity.Article;
import com.sqkj.entity.articleInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;

@Repository
@Mapper
public interface ArticleDao {
    @Select("select * from article")
    public List<Article> findAll();
    @Select("select * from article where id = #{id}")
    @Results({
            @Result(column="authorId",property="user",one=@One(
                    select="com.sqkj.dao.UserDao.findById",fetchType= FetchType.EAGER))
    })
    public Article findById(int id);
    @Insert("insert into article(articleTitle,authorId,articleViewCount,articleCreateTime,articleContent,articleGoodCnt,articleCommentCount,articleStatus,type,tag,articleAbstract,headImgUrl,audioUrl) values(#{articleTitle},#{authorId},#{articleViewCount},#{articleCreateTime},#{articleContent},#{articleGoodCnt},#{articleCommentCount},#{articleStatus},#{type},#{tag},#{articleAbstract},#{headImgUrl},#{audioUrl})")
    public int add(@Param("articleTitle")String articleTitle, @Param("authorId")int authorId, @Param("articleViewCount")int articleViewCount, @Param("articleCreateTime")String articleCreateTime,@Param("articleContent")String articleContent,@Param("articleGoodCnt")int articleGoodCnt,@Param("articleCommentCount")int articleCommentCount,@Param("articleStatus")int articleStatus,@Param("type")int type,@Param("tag")String tag, @Param("articleAbstract") String articleAbstract,@Param("headImgUrl")String headImgUrl,@Param("audioUrl")String audioUrl);
    @Delete("delete from article where id=#{id}")
    public int deleteById(int id);
    @Select("SELECT id,articleTitle,articleCreateTime ,headImgUrl FROM article  ORDER BY  RAND() LIMIT 5")
    public List<articleInfo> randFind();
    @Select("select * from article where authorId = #{authorId}")
    @Results({
            @Result(column="authorId",property="user",one=@One(
                    select="com.sqkj.dao.UserDao.findById",fetchType= FetchType.EAGER))
    })
    public List<Article> findAllByAuthorId(int authorId);
    @Update("update article set articleViewCount = #{articleViewCount} where id = #{id}")
    public int addViewCount(@Param("articleViewCount")int articleViewCount , @Param("id")int id);
    @Update("update article set articleGoodCnt = #{articleGoodCnt} where id = #{id}")
    public int addGoodCount(@Param("articleGoodCnt")int articleGoodCut,@Param("id") int id);
    @Update("update article set articleGoodCnt = #{articleGoodCnt} where id = #{id}")
    public int reduceGoodCount(@Param("articleGoodCnt")int articleGoodCut,@Param("id") int id);


}

